﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Common;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Filters;
using STF.Blog.WebApp.Areas.ForumManage.Models.SystemMenu;
using X.PagedList;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class SystemMenuController : Controller
    {
        private ISystemMenusService _sysmenu;
        public SystemMenuController(ISystemMenusService sysmenu)
        {
            _sysmenu = sysmenu;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search , int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _sysmenu.GetByNameAsync(search);

            var dataList = new List<SystemMenuListVIewModel>();

            foreach (var item in data)
            {
                var model = new SystemMenuListVIewModel
                {
                    Id = item.Id,
                    Title = item.Title,
                    Link = item.Link,
                    UpdateTime = item.UpdateTime,
                    ParentTitle = await GetParentTitle(item.ParentId)
                };
                dataList.Add(model);
            }

            ViewBag.Search = search;
            var pages = await dataList.ToPagedListAsync(page,PageConfig.PageSize);
            return View(pages);
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            return View(new SystemMenuAddViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> Add(SystemMenuAddViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                int parentId = 0;
                if (model.ParentList[0] == 2) 
                {
                    parentId = (int)model.ParentList[1];
                }
                else if (model.ParentList[0] == 3)
                {
                    parentId = (int)model.ParentList[2];
                }
                var res = await _sysmenu.AddUsersAsync(model.Title,model.Link,model.Icon,parentId);
                if (res > 0)
                    return Redirect("~/ForumManage/SystemMenu/Index");
            }

            return View(model);
        }

        [HttpGet]
        public async Task<IActionResult> Update(int id) 
        {
            var data = await _sysmenu.GetByIdAsync(id);

            if (data == null)
                return Redirect("~/ForumManage/SystemMenu/Index");

            var list = new List<int>();
            if (data.ParentId != 0)
            {
                var parentId = await _sysmenu.GetByIdAsync(data.ParentId);
                if (parentId.ParentId == 0)
                    list.AddRange(new int[] { 2, data.ParentId, 0 });
                else
                    list.AddRange(new int[] { 3, parentId.ParentId, data.ParentId });
            }
            else 
            {
                list.AddRange(new int[] { 1, 0, 0 });
            }

            return View(new SystemMenuUpdateViewModel 
            {
                Id = data.Id,
                Title = data.Title,
                Link = data.Link,
                Icon = data.Icon,
                ParentList = list
            });
        }

        [HttpPost]
        public async Task<IActionResult> Update(SystemMenuUpdateViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                int parentId = 0;
                if(model.ParentList[0] == 2)
                    parentId = model.ParentList[1];
                else if(model.ParentList[0] == 3)
                    parentId = model.ParentList[2];

                var res = await _sysmenu.UpdateUsersAsync(model.Id, model.Title, model.Link, model.Icon, parentId);
                if (res > 0)
                    return Redirect("~/ForumManage/SystemMenu/Index");
            }
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            await _sysmenu.PutTrashAsync(id);
            return Redirect("~/ForumManage/SystemMenu/Index");
        }

        [HttpGet]
        public async Task<IActionResult> GetSysMenuList(int parentId = 0) 
        {
            var data = await _sysmenu.GetByParentIdAsync(parentId);
            var dataList = data.Select(sm => new SystemMenuDropdownViewModel 
            {
                Id = sm.Id,
                Title = sm.Title
            }).ToList();

            return Json(dataList);
        }

        /// <summary>
        /// 查询父级菜单名称的方法
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private async Task<string> GetParentTitle(int parentId) 
        {
            if (parentId == 0)
                return "顶级菜单";
            var data = await _sysmenu.GetByIdAsync(parentId);
            return data.Title;
        }
    }
}
